package TAIC.Google ; 
import java.io.*;
import java.util.*;

public class PicDegree implements Runnable {
	static HashMap < String , Double > cache = new HashMap < String, Double > () ;
	
	static {
		String s ;
		Double d ; 
		try{
		
		File file = new File ( "cache\\picdegree.txt" ) ;
		if ( ! file.exists () ) file.createNewFile () ;
		Scanner scanner = new Scanner ( file ) ;
		while ( scanner.hasNextDouble () ) {
			d = scanner.nextDouble () ;
			s = scanner.nextLine ().trim() ; 
			cache.put ( s , d ) ;
		}
		
		}catch ( Exception e ) {
			e.printStackTrace();
		}
	}
	
	public static void writeCache () {
		try {
			PrintStream fout = new PrintStream ( new File ( "cache\\picdegree.txt" ) ) ; 
			for ( Iterator < Map.Entry <String , Double> > it = cache.entrySet().iterator() ; it.hasNext () ; ){
				Map.Entry < String , Double > temp = it.next () ;
				fout.println ( temp.getValue ()  + " " + temp.getKey () ) ; 
			}
			fout.close () ;
		}catch ( Exception e ) {
			e.printStackTrace () ;
		}
	}

    static public void main ( String s [] ) {
		//System.out.println ( Long.valueOf ( "5100000000" ) ) ;
		(new PicDegree ( "the" )).run();
		writeCache () ;
    } 
    
    public PicDegree ( String s ) {
        query = s.trim() ; ;
    }

    public void run ( ) {
        double t1 = 0 , t2 = 0 ;
        double x ;  
		Double d = cache.get ( query ) ; 
		if ( d != null ) {
			value = d ;
			return ; 
		} else if ( java.util.regex.Pattern.compile( "<.*>" ).matcher ( query ).matches() ) {
			value = 0 ;
			return ;
		}
		
		value = 0.5 ; 
		cache.put ( query , value ) ; 
//		t1 = (new GImage ()).getImageCount ( query ) ;
//		t2 = (new GWeb () ).getWebCount ( query ) ;
//    	x = Math.log ( ( t1 + 10e-13 )/ ( t2 * 10 + 10e-9) ) ;
//		//value = ( t1 + 10e-13 ) / ( t2 * 6 + 10e-9) ;
//    	value = 1 / ( 1 + Math.exp ( - ( 2.78*x + 15.4 ) )) ;
//		//System.out.println ( t1 + " " + t2 + " " + x + " " + value ) ; 
		
    }
    
    public double getValue () {
        return value ; 
    }

    private String query ;
    private double value ;
}
